const http = require('http')
const path = require('path')
const fs = require('fs')
const mime = require('mime')

const server = http.createServer((req, res) => {
    // xxx/http
    let filePath = path.resolve(__dirname, path.join('www', req.url))
    console.log(filePath);

    // 判断路径是否有对应的文件
    if (fs.existsSync(filePath)) {
        // res.end(fs.readFileSync(filePath)) // 文件详情
        const contentType = mime.getType(filePath)
        res.setHeader('Content-Type', contentType)
        res.end(fs.readFileSync(filePath))
        // const isDir = fs.statSync(filePath).isDirectory()

    } else {
        res.end('404 not found')
    }
})

server.listen(8080, () => {
    console.log('server is running at http://127.0.0.1:8080')
})